home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 034a / twview82.zip / GSDATA.INC < prev    next >
Text File  |  1991-02-04  |  6KB  |  227 lines

  1. {GSDATA.INC}
  2. const
  3.   SectorFieldMarker = 'Sector data starts here...';
  4.   
  5. procedure writeSectors( var g : text; var ss : SectorArray );
  6. var
  7.   s : sector;
  8.   i : warpindex;
  9. begin
  10.   writeln( g, SectorFieldMarker);
  11.   for s := 1 to maxSector do
  12.     with ss[s] do
  13.       if (number <> Unexplored) or (PortType <> NotAPort) or (etc <> Nothing) then
  14.       begin
  15.         write( g, s : 5, number:3 );
  16.         for i := 1 to number do
  17.           write( g, data[i] : 5);
  18.         write( g, portType : 3 );
  19.         write( g, etc : 6 );
  20.         writeln( g );
  21.       end; {for if}
  22. end; {writeSectors}
  23.  
  24. procedure WriteDock( var g : text; d : integer );
  25. begin
  26.   writeln( g, 'SpaceDock is ', d : 5 );
  27. end;
  28.  
  29. procedure WriteNotes( var g : text; var n : NoteList );
  30. var
  31.   i : 1..MaxNote;
  32. begin
  33.   writeln( g, n.top, '  <- number of notes' );
  34.   if n.top > 0 then
  35.     for i := 1 to n.top do
  36.       writeln( g, n.data[ i ].reference : 5, ' ', n.data[i].info );
  37. end; {WriteNotes}
  38.  
  39. procedure WritePorts( var g : text; var p : PortList );
  40. var
  41.   i : 1..MaxPorts;
  42. begin
  43.   writeln( g, p.top, '  <- number of Port Infos' );
  44.   if p.top > 0 then
  45.     for i := 1 to p.top do
  46.       with p.data[i] do
  47.         writeln( g, where : 5, amts[ Fuel ] : 8, amts[ Organics ] : 8, amts[ Equipment ] : 8 );
  48. end; {WritePorts}
  49.  
  50. const
  51.  DataFileIdentifier = '::Tradewars Data file::';
  52.  
  53. procedure SaveData( var g : text; var Space : TheVoid );
  54. begin
  55.   writeln(g, DataFileIdentifier );
  56.   writeDock( g, Space.dock );
  57.   writeNotes( g, Space.notes );
  58.   writePorts( g, space.ports );
  59.   writeSectors( g, space.sectors );
  60.   close( g );
  61. end; {SaveData}
  62.  
  63. procedure InitSectors( var s : SectorArray );
  64. var
  65.   r : sector;
  66. begin
  67.   for r := 1 to maxSector do
  68.     with s[r] do
  69.       begin
  70.         number  := UnExplored;
  71.         portType:= NotAPort;
  72.         etc     := Nothing;
  73.       end; {for with}
  74. end; {Init Sectors}
  75.  
  76.  
  77. function bval( line : string; var n : integer) : boolean;
  78. { convert  nonnegative numeric value at front of line into n; return whether
  79. conversion was successful. }
  80. var
  81.   i : integer;
  82.   error : boolean;
  83.  
  84. begin
  85.   i := 1;
  86.   n := 0;
  87.   Error := true;
  88.   while ( i <= length( line ) ) do
  89.     if line[i] = ' ' then
  90.       i := i + 1
  91.     else if line[ i ] in ['0'..'9'] then
  92.       begin
  93.         Error := false;
  94.         n := 10 * n + ord( line[ i ] ) - ord('0');
  95.         i := i + 1;
  96.       end
  97.     else
  98.       i := length( line ) + 1;
  99.   bval := error;
  100. end;
  101.  
  102. procedure InitSpace( var s : TheVoid );
  103. begin
  104.   s.dock  := 0;
  105.   s.notes.top := 0;
  106.   s.ports.top := 0;
  107.   InitSectors( s.sectors );
  108. end;
  109.  
  110. procedure ReadSectors( var h : text; var ss : SectorArray );
  111. var
  112.   r : sector;
  113.   i : warpindex;
  114.   temp,
  115.   portcount,
  116.   sectorcount :integer;
  117.   line : string;
  118. begin
  119.   portcount := 0; sectorcount := 0;
  120.   readln( h, line);   { "Sector data starts here..." }
  121.   if line <> SectorFieldMarker then
  122.     writeln('Sector data missing?  Found ', line )
  123.   else
  124.     while not eof( h ) do
  125.       begin
  126.         read( h, r );
  127.         sectorcount := sectorcount + 1;
  128.         with ss[r] do
  129.           begin
  130.             read( h, number );
  131.             for i := 1 to number do
  132.               begin
  133.                 read( h, temp );
  134.                 if (temp >= 1) and (temp <= maxSector) then
  135.                   data[ i ] := temp
  136.                 else
  137.                   writeln('bad data for sector ', r);
  138.               end; {for}
  139.             read( h, portType );
  140.             if PortType <> NotAPort then
  141.               portcount := portcount + 1;
  142.             read( h, etc );
  143.           end; {with}
  144.         readln( h );
  145.       end; {while}
  146.   writeln('Total of ', sectorcount, ' sectors, ', portcount, ' ports.');
  147.   close( h );
  148. end; {ReadSectors}
  149.  
  150. procedure ReadDock( var f : text; var d : SectorIndex );
  151. const
  152.   skip = 12;   { "SpaceDock is" }
  153. var
  154.   i : integer;
  155.   ch : char;
  156. begin
  157.   for i := 1 to skip do read( f, ch);
  158.   readln( f, d );
  159.   if d = 0 then
  160.     writeln('Space Dock location unknown')
  161.   else
  162.     writeln('Space Dock location ', d );
  163. end; {ReadDock}
  164.  
  165. procedure ReadNotes( var f : text; var sn : NoteList );
  166. var
  167.   i : 1..MaxNote;
  168.   j : integer;
  169.   n : string;
  170. begin
  171.   readln( f, sn.top );
  172.   if sn.top > 0 then
  173.     for i := 1 to sn.top do
  174.       with sn.data[ i ] do
  175.         begin
  176.           readln( f, n );
  177.           if not bval( copy( n, 1, 5 ), j ) then
  178.             begin
  179.               reference := j;
  180.               info := copy( n, 7, NoteSize);
  181.             end
  182.           else
  183.             writeln('Error with note ', n );
  184.         end; {with}
  185.   writeln('Info for ', sn.top, ' notes ');
  186. end; {ReadNotes}
  187.  
  188. procedure ReadPorts( var f : text; var sp : PortList );
  189. var
  190.   i : 1..MaxPorts;
  191. begin
  192.   readln( f, sp.top );
  193.   if sp.top > 0 then
  194.     for i := 1 to sp.top do
  195.       with sp.data[ i ] do
  196.         readln( f, where, amts[ Fuel ], amts[ Organics ], amts[ Equipment ] );
  197.   writeln('Info for ', sp.top, ' ports');
  198. end; {ReadPorts}
  199.  
  200. procedure GetData( var s : TheVoid );
  201. var
  202.   h : text;
  203.   line,
  204.   filename : string;
  205. begin
  206.   InitSpace( space );
  207.   write('Name of data file?  ');
  208.   readln( filename );
  209.   if filename = '' then
  210.     writeln('Okay, initializing with empty space.')
  211.   else
  212.     begin
  213.       assign( h, filename );
  214.       reset( h );
  215.       readln( h, line );
  216.       if line <> DataFileIdentifier then
  217.         begin
  218.           writeln('This is not a trade wars database file!');
  219.           halt;
  220.         end; {Error}
  221.       ReadDock( h, Space.dock );
  222.       ReadNotes( h, Space.notes );
  223.       ReadPorts( h, space.ports );
  224.       ReadSectors( h, space.sectors );
  225.     end; {if filename <> '' }
  226. end; {GetData}
  227.